﻿using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using LiMinCRMPlugin.InterfaceDocking;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.Util;
using System.ComponentModel;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Http;
using System.Security.Policy;

namespace LiMinCRMPlugin
{

    /// <summary>
    /// 收款单操作插件
    /// </summary>
    /// <remarks>
    /// 功能：收款单在审核和删除后判断上游是否关联待认领收款清单，对待认领收款的已认领状态进行更新。
    /// 创建人：wzx
    /// 创建日期：2024.6.28
    /// 修改信息（1）：修改人，修改日期，修改内容
    /// </remarks>
    /// 
    [Description("收款单审核删除更新待认领收款")]
    [HotUpdate]
    public class ReceiveBillUpdatePendingCollectionList: AbstractOperationServicePlugIn
    {
        //OnPreparePropertys 数据加载前，确保需要的属性被加载
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            //编码
            e.FieldKeys.Add("FBillNo");

            //数据来源
            e.FieldKeys.Add("F_DEV_DataSrc");

            //流水编码
            e.FieldKeys.Add("F_QLTK_SerialCoding");
            //流水内码
            e.FieldKeys.Add("F_QLTK_FlowingID");

            


        }
        /// <summary>
        /// 事务结束后事件
        /// </summary>
        /// <param name="e"></param>
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);

            //读取全部的单据,for循环,转换成DynamicObject类型
            foreach (DynamicObject entity in e.DataEntitys)
            {
                string DataSrc = Convert.ToString(entity["F_DEV_DataSrc"]);//数据来源
                string FID = Convert.ToString(entity["Id"]);//内码                      
                string Number = entity["BillNo"].ToString();//编码
                string SerialCoding = Convert.ToString(entity["F_QLTK_SerialCoding"]);//流水编码
                string FlowingID = Convert.ToString(entity["F_QLTK_FlowingID"]);//流水内码
                //数据来源为CRM同步，且流水编码和内码不为空
                if (DataSrc == "CRM" && !SerialCoding.IsNullOrEmptyOrWhiteSpace() && !FlowingID.IsNullOrEmptyOrWhiteSpace())
                {
                    string UpPe = "";//更新sql
                    if (this.FormOperation.Operation.ToUpper() == "AUDIT")//审核更新待认领的已认领状态为true
                    {
                        //更新待认领收款清单的认领状态
                        UpPe = string.Format(@"/*dialect*/  update RDLV_t_PendingCollection set F_QLTK_Claimed='1' where FID='{0}'", FlowingID);
                        DBServiceHelper.Execute(this.Context, UpPe);
                        //更新收款单付款信息
                        string UpDRL = string.Format(@"/*dialect*/ update T_AR_RECEIVEBILL set F_QLTK_PayerAccount=F_RDLV_PayerAccount,F_QLTK_PayerBankAccount=F_RDLV_PayerBankAccount,
F_QLTK_PayerOpenBank=F_RDLV_PayerOpenBank
from RDLV_t_PendingCollection 
where RDLV_t_PendingCollection.FBILLNO=T_AR_RECEIVEBILL.F_QLTK_SerialCoding and T_AR_RECEIVEBILL.FID='{0}'
", FID);
                        DBServiceHelper.Execute(this.Context, UpDRL);
                    }
                    else if (this.FormOperation.Operation.ToUpper() == "DELETE")//审核更新待认领的已认领状态为false
                    {
                        //更新待认领收款清单的认领状态
                        UpPe = string.Format(@"/*dialect*/  update RDLV_t_PendingCollection set F_QLTK_Claimed='0' where FID='{0}'", FlowingID);
                        DBServiceHelper.Execute(this.Context, UpPe);
                    }
                }               
            }
        }
    }
}
